package com.example.indenture.team.controller;

import com.example.indenture.common.vo.Result;
import com.example.indenture.common.vo.Search;
import com.example.indenture.team.entity.Team;
import com.example.indenture.team.service.TeamService;
import com.github.pagehelper.PageInfo;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
public class TeamController {
    @Autowired
    private TeamService teamService;

    @PostMapping(value = "/team/insert", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Result<Team> insertTeam(@RequestBody Team team) {
        return teamService.insertTeam(team);
    }

    @DeleteMapping(value = "/team/delete/{tid}")
    public Result<Object> deleteTeam(@PathVariable Integer tid) {
        return teamService.deleteTeam(tid);
    }

    @PostMapping(value = "/team/update", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Result<Object> updateTeam(@RequestBody Team team) {
        return teamService.updateTeam(team);
    }

    @PostMapping(value = "/teams", consumes = MediaType.APPLICATION_JSON_VALUE)
    public PageInfo<Team> getTeamsBySearch(@RequestBody Search search, Integer rid) {
        return teamService.teamByRaceAndName(rid, search);
    }

    @PostMapping(value = "/team/addTeamUser", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Result<Object> addTeamUser(@RequestBody Receive receive) {
        return teamService.addTeamUser(receive.getUid(), receive.getTid(), receive.getRid());
    }

    @Data
    static class Receive {
        Integer vercode;
        Integer uid;
        Integer tid;
        Integer rid;
    }

    @RequestMapping(value = "/team/quitTeam")
    public Result<Object> quitTeam(@RequestParam Integer uid, @RequestParam Integer tid) {
        return teamService.quitTeam(tid, uid);
    }

    @PostMapping(value = "/team/teamListByUser/{uid}", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Result<List<Team>> teamListByUser(@PathVariable Integer uid) {
        return teamService.teamListByUser(uid);
    }

    @PostMapping(value = "/team/isInTeam", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Result<Object> isInTeam(Integer uid, Integer rid) {
        return teamService.isInTeam(uid, rid);
    }

    @RequestMapping("/team/get-team-vercode")
    public Team getTeamVerCode(Integer vercode) {
        if (vercode == null) {
            return null;
        }
        return teamService.getTeamByVercode(vercode);
    }

    @RequestMapping(value = "/team/ok", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Boolean ok(@RequestBody Receive2 receive) {
        return teamService.ok(receive.getVercode(),receive.getTid()).getVercode().equals(receive.getVercode());
    }
    @Data
    static class Receive2{
        Integer vercode;
        Integer tid;
    }



}
